var Child = {
	template: '<div class="noMsg"><img class="noMsg-svg" src="../../images/noMsg.png"><p class="noMsg-p">暂无信息</p></div>'
};
new Vue({
	el: '#app',
	// 选项
	data: {
		id: '',
		tid: '',
		isActive: '',
		category_name: '',
		path: '',
		topic: [],
		questionList: [],
		t: [],
		page: '1',
		isShow: false,
		cateH: '',
		isNoM: false
	},
	mounted: function() {
		this.init();
	},
	components: {
		// <my-component> 将只在父模板可用
		'my-component': Child
	},
	methods: {
		init: function() {
			this.getFid();
		},
		//获得分类话题id
		getFid: function() {
			var self = this;
			//self.getQuestionList();
			//self.getTopic();
			apiready = function() {
				var fid = api.pageParam;
				self.tid = fid.id;
				self.category_name = fid.name;
				//loading
				self.isS();
				api.addEventListener({
					name: 'scrolltobottom',
					extra: {
						threshold: 0 //设置距离底部多少距离时触发，默认值为0，数字类型
					}
				}, function(ret, err) {
					//alert('已滚动到底部');
					self.page++;
					if(self.page > self.pageCount) {
						api.toast({
							msg: '没有更多了',
							duration: 1000,
							location: 'bottom'
						});
					} else {
						self.getQuestionList(self.id);
					}
				});
			};
		},
		isS: function() {
			var self = this;
			var UILoading = api.require('UILoading');
			if(self.isShow) {
				console.log(self.loadingId);
				UILoading.closeFlower({
					id: self.loadingId
				});
			} else {
				UILoading.flower({
					center: {
						x: api.winWidth / 2.0,
						y: api.winHeight / 2.0
					},
					size: 30,
					fixed: true
				}, function(ret) {
					console.log(JSON.stringify(ret));
					self.loadingId = ret.id;
				});
				if(self.id) {

				} else {
					self.getTopic();
				}
				self.getQuestionList();
			}
		},
		//是否有数据
		isNoMsg: function(d) {
			console.log(d);
			if(d.length == 0) {
				this.isNoM = true;
			}else{
				this.isNoM = false;
			}
		},
		//获得话题
		getTopic: function() {
			var self = this;
			var data = {
				category_id: self.tid
			}
			$.post(base + config.sns_topic_index, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.topic = msg.data;
					self.$nextTick(function() {
						// DOM 现在更新了
						// `this` 绑定到当前实例
						self.cateH = $("#cate").height();
					});
				} else {
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');

		},
		//获得问题列表
		getQuestionList: function() {
			var self = this;
			console.log(self.id);
			var data = {
				category_id: self.tid,
				topic_id: self.id,
				page: self.page
			};
			$.post(base + config.sns_question_index, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.t = msg.data;
					for(var i = 0; i < self.t.length; i++) {
						self.questionList.push(self.t[i]);
					};
					self.pageCount = msg.page_count;
					self.path = msg.url;
					self.$nextTick(function() {
						// DOM 现在更新了
						// `this` 绑定到当前实例
						console.log(self.cateH);
						$("#main").css("margin-top", (self.cateH + 75 + 20) + "px");
					});
					self.isNoM = false;
					self.isShow = true;
					self.isS();
				} else {
					self.isShow = true;
					self.isS();
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
					self.isNoM = true;
				}
			}, 'json');
		},
		//跳转到该分类话题下的问题列表
		jump: function(id) {
			console.log(id);
			this.isActive = id;
			this.id = id;
			this.page = "1";
			this.questionList = [];
			this.isShow = false;
			this.isS();
		},
		//跳转到该问题的详情
		jumpDetails: function(id, u_id) {
			api.openWin({
				name: 'question-details',
				url: 'questionDetails.html',
				vScrollBarEnabled: false,
//				reload: true,
				pageParam: {
					qid: id,
					u_id: u_id,
					back: 2
				}
			});
		}

	}
})